package com.example.homework7;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SampleAdapter extends BaseAdapter {

	private Activity activity;
	private List<Content> contentList;
	private final int ID_IMAGEVIEW = 1;
	private final int ID_TITLE_TEXTVIEW = 2;
	private final int ID_DESCRIPTION_TEXTVIEW = 3;
	
	public SampleAdapter(Activity activity, List<Content> contentList) {
		super();
		this.activity = activity;
		this.contentList = contentList;
	}

	@Override
	public int getCount() {
		return this.contentList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.contentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = this.createItemView(parent.getContext());
			
			LayoutInflater layoutInflater = this.activity.getLayoutInflater();
			convertView = layoutInflater.inflate(R.layout.item_view, parent, false);
			
		}
		
		TextView titleTextView = (TextView) convertView.findViewById(R.id.textView1);
		
		Content content = this.contentList.get(position);
		
		titleTextView.setText(content.getName());
		
		return convertView;
	}

	private View createItemView(Context context) {
		RelativeLayout relativeLayout = new RelativeLayout(context);
		
		ImageView imageView = new ImageView(context);
		
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
		layoutParams.leftMargin = 10;
		layoutParams.topMargin = 10;
		layoutParams.rightMargin = 10;
		layoutParams.bottomMargin = 10;
		imageView.setLayoutParams(layoutParams);
		imageView.setId(ID_IMAGEVIEW);
		
		relativeLayout.addView(imageView);
		
		TextView titleTextView = new TextView(context);
		layoutParams = new RelativeLayout.LayoutParams(300, 50);
		layoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGEVIEW);
		titleTextView.setLayoutParams(layoutParams);
		titleTextView.setId(ID_TITLE_TEXTVIEW);
		
		relativeLayout.addView(titleTextView);

		TextView descriptionTextView = new TextView(context);
		layoutParams = new RelativeLayout.LayoutParams(300, 50);
		layoutParams.addRule(RelativeLayout.RIGHT_OF, ID_IMAGEVIEW);
		layoutParams.addRule(RelativeLayout.BELOW, ID_TITLE_TEXTVIEW);
		descriptionTextView.setLayoutParams(layoutParams);
		descriptionTextView.setId(ID_DESCRIPTION_TEXTVIEW);
		
		relativeLayout.addView(descriptionTextView);
		
		return relativeLayout;
	}
}
